package net.zzy.pbbot.plugin.zfbSign.scheduled;

import lombok.extern.slf4j.Slf4j;
import net.lz1998.pbbot.bot.Bot;
import net.lz1998.pbbot.bot.BotContainer;
import net.lz1998.pbbot.utils.Msg;
import net.zzy.pbbot.plugin.zfbSign.entity.ZfbSignDaoEntity;
import net.zzy.pbbot.plugin.zfbSign.mapper.ZfbSignMapper;
import net.zzy.pbbot.plugin.zfbSign.service.ZfbSignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

import static net.zzy.pbbot.finalfield.FinalInt.BOT_QQ;

/**
 * @author zhuZhaoYang
 * @date 2021/10/6 0:03
 */
@Component
@Slf4j
public class EveryDayAutoSign {
    private BotContainer botContainer;

    @Autowired
    public void setBotContainer(BotContainer botContainer) {
        this.botContainer = botContainer;
    }

    private ZfbSignMapper zfbSignMapper;

    @Autowired
    public void setZfbSignMapper(ZfbSignMapper zfbSignMapper) {
        this.zfbSignMapper = zfbSignMapper;
    }

    private ZfbSignService zfbSignService;

    @Autowired
    public void setZfbSignService(ZfbSignService zfbSignService) {
        this.zfbSignService = zfbSignService;
    }


    @Scheduled(cron = "0 0 10 * * ?")
    public void signAndSendMsg() {
        Bot bot = botContainer.getBots().get(BOT_QQ);
        if (bot == null) {
            log.error("Bot为空,BotQQ号为:{},请确认当前登录Bot是否正确。", BOT_QQ);
            return;
        }
        List<ZfbSignDaoEntity> zfbSignDaoEntities = zfbSignMapper.selectList(null);
        for (ZfbSignDaoEntity zfbSignDaoEntity : zfbSignDaoEntities) {
            Msg msg = zfbSignService.loginAndSave(zfbSignDaoEntity.getQq());
            msg.text("\n\t\t每日定时打卡结果。");
            msg.sendToFriend(bot, Long.parseLong(zfbSignDaoEntity.getQq()));
        }
    }

}
